/**
 * 
 */
package microcredit.gui.table.impl;

import java.awt.Color;
import java.math.BigDecimal;
import java.util.Date;

import javax.swing.ImageIcon;

import microcredit.gui.table.EnumBeanTableModel;
import microcredit.gui.util.FormatUtilities;
import microcredit.model.MemberStatusHistory;
import microcredit.model.MemberToLoan;

enum MemberToLoanColumns {
	COLUMN_NUMBER,
	COLUMN_AMOUNT,
	COLUMN_RETURN_AMOUNT,
	COLUMN_AMOUNT_PAID,
	COLUMN_STATUS,
	
	COLUMN_START_DATE,
	COLUMN_END_DATE,
}

public final class MemberToLoanTableModel extends
		EnumBeanTableModel<MemberToLoan, MemberToLoanColumns> {

	@Override
	protected Object getValue(MemberToLoan bean, MemberToLoanColumns columnIndex) {
		switch (columnIndex) {
		case COLUMN_NUMBER : 
			return MemberToLoan.formatID(bean);
		case COLUMN_AMOUNT : 
			return bean.getLoanAmount();
		case COLUMN_RETURN_AMOUNT : 
			return bean.getReturnAmount();
		case COLUMN_AMOUNT_PAID : 
			return bean.getAmountPaid();
		case COLUMN_STATUS : 
			return bean.getLoanStatus() != null ? bean.getLoanStatus().getString() : "Invalid status";
		case COLUMN_START_DATE : 
			return bean.getStartDate();
		case COLUMN_END_DATE : 
			return bean.getFinalizedDate();
		}
		return null;
	}
	
	@Override
	protected ImageIcon getRowIcon(MemberToLoan o) {
		String icon = "";
		switch (o.getLoanStatus()) {
		case A :
			icon = "clock_run";
			break;
		case F :
			icon = "clock_preferences";
			break;
		case K :
			icon = "clock_preferences";
			break;
		case N :
			icon = "clock_stop";
			break;
		case R :
			icon = "clock_run";
			break;
		}
		return new ImageIcon(MemberStatusHistory.class
				.getResource("/microcredit/icons/x16/"+icon+".png"));
	}
	
	@Override
	protected MemberToLoanColumns getSortColumn() {
		return MemberToLoanColumns.COLUMN_NUMBER;
	}
	
	@Override
	protected Color getForeground(MemberToLoan o, MemberToLoanColumns column) {
		switch (o.getLoanStatus()) {
		case A :
			return Color.BLUE;
		case F :
			return null;
		case K :
			return null;
		case N :
			return Color.RED;
		case R :
			return Color.BLUE;
		}
		return super.getForeground(o, column);
	}
	
	@Override
	protected boolean isBold(MemberToLoan o, MemberToLoanColumns column) {
		return !o.isFinalized();
	}
	
	@Override
	protected Class<?> getValueClass(MemberToLoanColumns column) {
		switch (column) {
		case COLUMN_AMOUNT : return BigDecimal.class;
		case COLUMN_AMOUNT_PAID : return BigDecimal.class;
		case COLUMN_RETURN_AMOUNT : return BigDecimal.class;
		case COLUMN_START_DATE : return Date.class;
		case COLUMN_END_DATE : return Date.class;
		}
		return super.getValueClass(column);
	}
	
	@Override
	protected Object getFormat(MemberToLoan o, MemberToLoanColumns column) {
		switch (column) {
		case COLUMN_AMOUNT : return FormatUtilities.getMoneyFormat();
		case COLUMN_AMOUNT_PAID : return FormatUtilities.getMoneyFormat();
		case COLUMN_RETURN_AMOUNT : return FormatUtilities.getMoneyFormat();
		case COLUMN_START_DATE : return FormatUtilities.getDateFormat();
		case COLUMN_END_DATE : return FormatUtilities.getDateFormat();
		}
		return null;
	}
	
	@Override
	protected String getName(MemberToLoanColumns column) {
		switch (column) {
		case COLUMN_NUMBER : return "Number";
		case COLUMN_AMOUNT : return "Loan";
		case COLUMN_RETURN_AMOUNT : return "To Return";
		case COLUMN_AMOUNT_PAID : return "Returned";
		case COLUMN_STATUS : return "Status";
		case COLUMN_START_DATE : return "Start date";
		case COLUMN_END_DATE : return "End date";
		}
		return null;
	}

}